package com.cloudsigns.factorymanager2.operate_middle_level.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cloudsigns.factorymanager2.LoginActivity;
import com.cloudsigns.factorymanager2.MyApplicaton;
import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.bean.AccountInfoBean;
import com.cloudsigns.factorymanager2.engineering.fragment.BaseFragmentActivity;
import com.cloudsigns.factorymanager2.operate_middle_level.fragment.ManageFragment;
import com.cloudsigns.factorymanager2.operate_middle_level.fragment.OfficeFragment;
import com.cloudsigns.factorymanager2.operate_middle_level.fragment.PanelMapFragment;
import com.cloudsigns.factorymanager2.produce.activity.AvatarImageShowerActivity;
import com.cloudsigns.factorymanager2.project.adapter.ProjectMainViewPageAdapter;
import com.cloudsigns.factorymanager2.project.customview.CustomViewPager;
import com.cloudsigns.factorymanager2.project.customview.DropDownPoupWindow;
import com.cloudsigns.factorymanager2.project.customview.GlideCircleTransform;
import com.cloudsigns.factorymanager2.project.customview.PageIndicatorView;
import com.cloudsigns.factorymanager2.project.fragment.SettingFragmet;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;

public class OperateMiddleLevelFragmentActivity extends BaseFragmentActivity implements View.OnClickListener {
    private final String TAG = OperateMiddleLevelFragmentActivity.class.getSimpleName();
    private View ll_op_one, ll_op_two, ll_op_three, ll_op_four;
    private ImageView img_op_one, img_op_two, img_op_three, img_op_four;
    private ImageView img_tri_one, img_tri_two, img_tri_three, img_tri_four;
    private RelativeLayout rl_back;
    private View rl_head;
    private TextView tv_title;
    private TextView tv_headtop, tv_headbottom;
    private ImageView img_head;
    private CustomViewPager viewpager;
    private ArrayList<Fragment> listFragments;
    private ProjectMainViewPageAdapter adapter;

    private RelativeLayout layout_panel, layout_management, layout_office, layout_system;
    private ImageView img_panel, img_management, img_office, img_system;

    private Fragment managementFragment, offficeFragemnt, systemFragment;
    private PanelMapFragment panelMapFragment;
    private PageIndicatorView indicator;

    /*底部四个tab选项*/
    public static final int PANEL = 0;// 面板
    public static final int MANAGEMENT = 1;// 管理
    public static final int OFFICE = 2;// 办公
    public static final int SYSTEM = 3;// 系统
    /*当前选中的tab*/
    public int currentTab = PANEL;

    private DropDownPoupWindow popupWindow;
    /*标题栏三个按钮类型*/
    public static final int MODULE1 = 0;
    public static final int MODULE2 = 1;
    public static final int MODULE3 = 3;

    /*值班管控*/
    public static final int DUTY_CONTROL = 0;
    /*值班巡查*/
    public static final int DUTY_INSPECTION = 1;
    /*广告上刊*/
    public static final int PUBLICATION = 2;
    /*工程施工*/
    public static final int ENGINEERING_CONSTRUCTION = 3;

    /*标题栏 第一个按钮 当前选中的功能*/
    public int currentModuleType = DUTY_CONTROL;
    private AccountInfoBean accountInfoBean;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_operate_middle_level);

        init();
        tv_title.setText("");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent != null) {
            boolean isExit = intent.getBooleanExtra(ConstantUtil.KEY_1, false);
            if (isExit) {
                Intent mItent = new Intent(this, LoginActivity.class);
                startActivity(mItent);
                this.finish();
            }
        }
    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        ll_op_one = findViewById(R.id.ll_op_one);
        ll_op_two = findViewById(R.id.ll_op_two);
        ll_op_two = findViewById(R.id.ll_op_two);
        ll_op_three = findViewById(R.id.ll_op_three);
        ll_op_four = findViewById(R.id.ll_op_four);
        img_op_one = findViewById(R.id.img_op_one);
        img_op_two = findViewById(R.id.img_op_two);
        img_op_three = findViewById(R.id.img_op_three);
        img_op_four = findViewById(R.id.img_op_four);
        img_tri_one = findViewById(R.id.img_tri_one);
        img_tri_two = findViewById(R.id.img_tri_two);
        img_tri_three = findViewById(R.id.img_tri_three);
        img_tri_four = findViewById(R.id.img_tri_four);
        rl_head = findViewById(R.id.rl_head);
        tv_headtop = findViewById(R.id.tv_headtop);
        tv_headbottom = findViewById(R.id.tv_headbottom);
        img_panel = findViewById(R.id.img_panel);
        img_management = findViewById(R.id.img_management);
        img_office = findViewById(R.id.img_office);
        img_system = findViewById(R.id.img_system);
        img_head = findViewById(R.id.img_head);

        layout_panel = findViewById(R.id.layout_panel);
        layout_management = findViewById(R.id.layout_management);
        layout_office = findViewById(R.id.layout_office);
        layout_system = findViewById(R.id.layout_system);
        viewpager = findViewById(R.id.viewpager);
        viewpager.setScanScroll(false);
        indicator = findViewById(R.id.indicator);

        rl_head.setVisibility(View.VISIBLE);

        rl_back.setVisibility(View.GONE);
        img_head.setOnClickListener(this);
        layout_panel.setOnClickListener(this);
        layout_management.setOnClickListener(this);
        layout_office.setOnClickListener(this);
        layout_system.setOnClickListener(this);
        ll_op_one.setOnClickListener(this);
        ll_op_two.setOnClickListener(this);
        ll_op_three.setOnClickListener(this);
        ll_op_four.setOnClickListener(this);

        accountInfoBean = MyApplicaton.getApplication().getAccountInfoBean();
        if (null != accountInfoBean) {
            if (accountInfoBean.getRoleCategory() == CommonEnum.ROLECATEGORY.MANAGER) {
                tv_headbottom.setText("工程管理/" + accountInfoBean.getAccountName());

                String companyName = accountInfoBean.getCompanyName();
                if (!TextUtils.isEmpty(companyName) && !"null".equals(companyName)) {
                    tv_headtop.setText(companyName);
                }
                Glide.with(this).load(accountInfoBean.getAvatar()).bitmapTransform(new GlideCircleTransform(this)).crossFade(1000).error(R.mipmap.default_avatar).into(img_head);
            }
        }

        panelMapFragment = new PanelMapFragment();
        managementFragment = new ManageFragment();
        offficeFragemnt = new OfficeFragment();
        systemFragment = new SettingFragmet();

        listFragments = new ArrayList<Fragment>();
        listFragments.add(panelMapFragment);
        listFragments.add(managementFragment);
        listFragments.add(offficeFragemnt);
        listFragments.add(systemFragment);

        adapter = new ProjectMainViewPageAdapter(getSupportFragmentManager(), listFragments);
        viewpager.setAdapter(adapter);
        viewpager.setOffscreenPageLimit(4);

        indicator.setViewPager(viewpager);
        indicator.setFades(false);
        indicator.setSelectedColor(getResources().getColor(R.color.rgb_17a5ef));
        viewpager.setCurrentItem(PANEL);
        switchTab(PANEL);

        indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                switchTab(position);
                currentTab = position;

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });

        img_op_one.setBackgroundResource(R.mipmap.title_icon_menu);
        img_op_two.setBackgroundResource(R.mipmap.icon_hand);
        img_op_three.setBackgroundResource(R.mipmap.icon_produced);
        img_op_four.setBackgroundResource(R.mipmap.icon_patrol);

        ll_op_one.setVisibility(View.VISIBLE);
        ll_op_two.setVisibility(View.VISIBLE);
        ll_op_three.setVisibility(View.GONE);
        ll_op_four.setVisibility(View.VISIBLE);

    }

    private void switchTab(int pos) {
        // TODO Auto-generated method stub
        switch (pos) {
            // 面板
            case PANEL:
                layout_panel.setSelected(true);
                layout_panel.setBackgroundColor(getResources().getColor(R.color.rgb_17a5ef));
                img_panel.setImageDrawable(getResources().getDrawable(R.mipmap.icon_engineering_management_pressed));

                layout_management.setSelected(false);
                layout_management.setBackgroundColor(getResources().getColor(R.color.color_f7f7f7));
                img_management.setImageDrawable(getResources().getDrawable(R.mipmap.tab_project_order));

                layout_office.setSelected(false);
                layout_office.setBackgroundColor(getResources().getColor(R.color.color_f7f7f7));
                img_office.setImageDrawable(getResources().getDrawable(R.mipmap.tab_project_office));

                layout_system.setSelected(false);
                layout_system.setBackgroundColor(getResources().getColor(R.color.color_f7f7f7));
                img_system.setImageDrawable(getResources().getDrawable(R.mipmap.tab_project_systems));

                tv_title.setText("");

                img_op_one.setBackgroundResource(R.mipmap.title_icon_menu);
                img_op_two.setBackgroundResource(R.mipmap.icon_hand);
                img_op_three.setBackgroundResource(R.mipmap.icon_produced);
                img_op_four.setBackgroundResource(R.mipmap.icon_patrol);


                ll_op_one.setVisibility(View.VISIBLE);
                ll_op_two.setVisibility(View.VISIBLE);
                ll_op_three.setVisibility(View.GONE);
                ll_op_four.setVisibility(View.VISIBLE);

                rl_head.setVisibility(View.VISIBLE);
                rl_back.setVisibility(View.GONE);
                break;

            // 管理
            case MANAGEMENT:
                layout_panel.setSelected(false);
                layout_panel.setBackgroundColor(getResources().getColor(R.color.color_f7f7f7));
                img_panel.setImageDrawable(getResources().getDrawable(R.mipmap.icon_engineering_management));

                layout_management.setSelected(true);
                layout_management.setBackgroundColor(getResources().getColor(R.color.rgb_17a5ef));
                img_management.setImageDrawable(getResources().getDrawable(R.mipmap.tab_project_order_pressed));

                layout_office.setSelected(false);
                layout_office.setBackgroundColor(getResources().getColor(R.color.color_f7f7f7));
                img_office.setImageDrawable(getResources().getDrawable(R.mipmap.tab_project_office));

                layout_system.setSelected(false);
                layout_system.setBackgroundColor(getResources().getColor(R.color.color_f7f7f7));
                img_system.setImageDrawable(getResources().getDrawable(R.mipmap.tab_project_systems));

                tv_title.setText("");

                ll_op_one.setVisibility(View.GONE);
                ll_op_two.setVisibility(View.GONE);
                ll_op_three.setVisibility(View.GONE);
                ll_op_four.setVisibility(View.GONE);

                rl_head.setVisibility(View.VISIBLE);
                rl_back.setVisibility(View.GONE);

                break;

            // 办公
            case OFFICE:
                layout_panel.setSelected(false);
                layout_panel.setBackgroundColor(getResources().getColor(R.color.color_f7f7f7));
                img_panel.setImageDrawable(getResources().getDrawable(R.mipmap.icon_engineering_management));

                layout_management.setSelected(false);
                layout_management.setBackgroundColor(getResources().getColor(R.color.color_f7f7f7));
                img_management.setImageDrawable(getResources().getDrawable(R.mipmap.tab_project_order));

                layout_office.setSelected(true);
                layout_office.setBackgroundColor(getResources().getColor(R.color.rgb_17a5ef));
                img_office.setImageDrawable(getResources().getDrawable(R.mipmap.tab_project_office_pressed));

                layout_system.setSelected(false);
                layout_system.setBackgroundColor(getResources().getColor(R.color.color_f7f7f7));
                img_system.setImageDrawable(getResources().getDrawable(R.mipmap.tab_project_systems));

                tv_title.setText("");

                ll_op_one.setVisibility(View.GONE);
                ll_op_two.setVisibility(View.GONE);
                ll_op_three.setVisibility(View.GONE);
                ll_op_four.setVisibility(View.GONE);

                rl_head.setVisibility(View.VISIBLE);
                rl_back.setVisibility(View.GONE);

                break;

            // 系统
            case SYSTEM:
                layout_panel.setSelected(false);
                layout_panel.setBackgroundColor(getResources().getColor(R.color.color_f7f7f7));
                img_panel.setImageDrawable(getResources().getDrawable(R.mipmap.icon_engineering_management));

                layout_management.setSelected(false);
                layout_management.setBackgroundColor(getResources().getColor(R.color.color_f7f7f7));
                img_management.setImageDrawable(getResources().getDrawable(R.mipmap.tab_project_order));

                layout_office.setSelected(false);
                layout_office.setBackgroundColor(getResources().getColor(R.color.color_f7f7f7));
                img_office.setImageDrawable(getResources().getDrawable(R.mipmap.tab_project_office));

                layout_system.setSelected(true);
                layout_system.setBackgroundColor(getResources().getColor(R.color.rgb_17a5ef));
                img_system.setImageDrawable(getResources().getDrawable(R.mipmap.tab_project_systems_pressed));

                tv_title.setText("");

                ll_op_one.setVisibility(View.GONE);
                ll_op_two.setVisibility(View.GONE);
                ll_op_three.setVisibility(View.GONE);
                ll_op_four.setVisibility(View.GONE);

                rl_head.setVisibility(View.VISIBLE);
                rl_back.setVisibility(View.GONE);

                break;
        }
    }

    /**
     * 功能1
     *
     * @param anchor
     */
    private void showModulePopupWindow(View anchor) {
        if (null != popupWindow)
            popupWindow.dismiss();

        popupWindow = new DropDownPoupWindow(this, TAG, MODULE1, R.layout.popwindow_operate_maintain_module1, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        //  backgroundAlpha(0.6f);
        img_tri_one.setVisibility(View.VISIBLE);

        popupWindow.setOnClickListener(this);
        popupWindow.setCusOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
                img_tri_one.setVisibility(View.GONE);
            }
        });
        popupWindow.showPopupWindow(anchor, anchor.getLayoutParams().width / 2, 5);
    }

    /**
     * 功能2
     *
     * @param anchor
     */
    private void showEngineeringPopupWindwo(View anchor) {
        if (null != popupWindow)
            popupWindow.dismiss();

        popupWindow = new DropDownPoupWindow(this, TAG, MODULE2, R.layout.popwindow_operate_maintain_module2, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        // backgroundAlpha(0.6f);
        img_tri_two.setVisibility(View.VISIBLE);
        popupWindow.setOnClickListener(this);
        popupWindow.setCusOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
                img_tri_two.setVisibility(View.GONE);
            }
        });
        popupWindow.showPopupWindow(anchor, anchor.getLayoutParams().width / 2, 5);
    }

    /**
     * 功能4
     *
     * @param anchor
     */
    private void showSelectPopupWindwo(View anchor) {
        if (null != popupWindow)
            popupWindow.dismiss();

        popupWindow = new DropDownPoupWindow(this, TAG, MODULE3, R.layout.popwindow_map_for_manager_selectproject, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        // backgroundAlpha(0.6f);
        img_tri_four.setVisibility(View.VISIBLE);
        popupWindow.setOnClickListener(this);
        popupWindow.setCusOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
                img_tri_four.setVisibility(View.GONE);
            }
        });
        popupWindow.showPopupWindow(anchor, anchor.getLayoutParams().width / 2, 5);
    }


    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    private void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);
    }

    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            // 头像
            case R.id.img_head:
                Intent intent = new Intent(OperateMiddleLevelFragmentActivity.this, AvatarImageShowerActivity.class);
                intent.putExtra("imageUrl", accountInfoBean.getAvatar());
                startActivity(intent);
                break;

            /*标题栏三个popupWindow按钮类型*/
            case R.id.ll_op_one:
                showModulePopupWindow(view);
                break;
            case R.id.ll_op_two:
                showEngineeringPopupWindwo(view);
                break;
            case R.id.ll_op_four:
                showSelectPopupWindwo(view);
                break;

            /*底部四个tab选项*/
            // 面板
            case R.id.layout_panel:
                if (currentTab != PANEL) {
                    currentTab = PANEL;
                    viewpager.setCurrentItem(PANEL);
                    switchTab(PANEL);
                }
                break;
            // 管理
            case R.id.layout_management:
                if (currentTab != MANAGEMENT) {
                    currentTab = MANAGEMENT;
                    viewpager.setCurrentItem(MANAGEMENT);
                    switchTab(MANAGEMENT);
                }
                break;
            // 办公
            case R.id.layout_office:
                if (currentTab != OFFICE) {
                    currentTab = OFFICE;
                    viewpager.setCurrentItem(OFFICE);
                    switchTab(OFFICE);
                }
                break;
            // 系统
            case R.id.layout_system:
                if (currentTab != SYSTEM) {
                    currentTab = SYSTEM;
                    viewpager.setCurrentItem(SYSTEM);
                    switchTab(SYSTEM);
                }
                break;


            /*popupwindow按钮1点击*/
            // 值班管控
            case R.id.ll_duty_control:
                currentModuleType = DUTY_CONTROL;
                panelMapFragment.getOrderList(currentModuleType);
                break;
            // 值班巡查
            case R.id.ll_duty_inspection:
                currentModuleType = DUTY_INSPECTION;
                panelMapFragment.getOrderList(currentModuleType);
                break;
            // 广告上刊
            case R.id.ll_publication:
                currentModuleType = PUBLICATION;
                panelMapFragment.getOrderList(currentModuleType);
                break;
            // 工程施工
            case R.id.ll_construction:
                currentModuleType = ENGINEERING_CONSTRUCTION;
                panelMapFragment.getOrderList(currentModuleType);
                break;


            /*popupwindow按钮2点击*/
            // 工作计划
            case R.id.ll_work_plan2:
                break;
            // 值班巡查
            case R.id.ll_duty_inspection2:
                break;
            // 广告上刊
            case R.id.ll_publication2:
//                mIntent = new Intent(OperateMaintainFragmentActivity.this, MediaSystemPublicationStatusFormActivity.class);
//                startActivity(mIntent);
                break;
            // 维修
            case R.id.ll_maintenance2:
//                mIntent = new Intent(OperateMaintainFragmentActivity.this, MediaSystemMaintenanceStatusFormActivity.class);
//                startActivity(mIntent);
                break;
            // 维保
            case R.id.ll_service:
//                mIntent = new Intent(OperateMaintainFragmentActivity.this, MediaSystemServiceStatusFormActivity.class);
//                startActivity(mIntent);
                break;
            // 工程施工
            case R.id.ll_construct:
//                mIntent = new Intent(OperateMaintainFragmentActivity.this, MediaSystemEngineeringConstructStatusFormActivity.class);
//                startActivity(mIntent);
                break;


        }

    }
}
